import CryptoJS from 'crypto-js';
const iv = CryptoJS.enc.Utf8.parse('12345678');
const encrypt = (f, e) => {
  var h = CryptoJS.enc.Utf8.parse(e);
  var g = CryptoJS.DES.encrypt(f, h, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  var obj = {
    data: g.toString()
  };

  return obj;
};
const encrypt_cydn = (f, e) => {
  var h = CryptoJS.enc.Utf8.parse(e);
  var g = CryptoJS.DES.encrypt(f, h, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  var obj = {
    dataBody: g.toString()
  };

  return obj;
};
// des加密 g=明文 ，f=key
const decrypt = (g, f) => {
  var h = CryptoJS.enc.Utf8.parse(f);
  var e = CryptoJS.DES.decrypt(
    {
      ciphertext: CryptoJS.enc.Base64.parse(g)
    },
    h,
    {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    }
  );
  return e.toString(CryptoJS.enc.Utf8);
};
const getUrlParam = (name) => {
  const queryStr = String(window.location).split('?')[1] || ''
  name = encodeURIComponent(name)
  let arr = new RegExp("(^|&)" + name + "=([^&]*)(&|$)").exec(queryStr);
  if (arr) {
    return RegExp.$2;
  } else {
    return '';
  }
}
export { decrypt, encrypt, getUrlParam,encrypt_cydn };
